/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.classnet.db;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author karltinawi
 */
@Entity
@Table(name = "course")
@NamedQueries({@NamedQuery(name = "Course.findBySubject", query = "SELECT c FROM Course c WHERE c.coursePK.subject = :subject"), @NamedQuery(name = "Course.findByCode", query = "SELECT c FROM Course c WHERE c.coursePK.code = :code"), @NamedQuery(name = "Course.findBySection", query = "SELECT c FROM Course c WHERE c.coursePK.section = :section"), @NamedQuery(name = "Course.findByTitle", query = "SELECT c FROM Course c WHERE c.title = :title")})
public class Course implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected CoursePK coursePK;
    @Column(name = "title", nullable = false)
    private String title;
    @ManyToMany(mappedBy = "courseCollection")
    private Collection<User> userCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "course")
    private Collection<CourseThread> courseThreadCollection;
    @JoinColumn(name = "subject", referencedColumnName = "code", insertable = false, updatable = false)
    @ManyToOne
    private Subject subject1;

    public Course() {
    }

    public Course(CoursePK coursePK) {
        this.coursePK = coursePK;
    }

    public Course(CoursePK coursePK, String title) {
        this.coursePK = coursePK;
        this.title = title;
    }

    public Course(String subject, String code, String section) {
        this.coursePK = new CoursePK(subject, code, section);
    }

    public CoursePK getCoursePK() {
        return coursePK;
    }

    public void setCoursePK(CoursePK coursePK) {
        this.coursePK = coursePK;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Collection<User> getUserCollection() {
        return userCollection;
    }

    public void setUserCollection(Collection<User> userCollection) {
        this.userCollection = userCollection;
    }

    public Collection<CourseThread> getCourseThreadCollection() {
        return courseThreadCollection;
    }

    public void setCourseThreadCollection(Collection<CourseThread> courseThreadCollection) {
        this.courseThreadCollection = courseThreadCollection;
    }

    public Subject getSubject1() {
        return subject1;
    }

    public void setSubject1(Subject subject1) {
        this.subject1 = subject1;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (coursePK != null ? coursePK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Course)) {
            return false;
        }
        Course other = (Course) object;
        if ((this.coursePK == null && other.coursePK != null) || (this.coursePK != null && !this.coursePK.equals(other.coursePK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.classnet.db.Course[coursePK=" + coursePK + "]";
    }

}
